﻿using System;
using System.Collections.Generic;
using BCCommon;
using BCCommon.Extensions;
using BCEntity.OA.Attendance.AttendanceClass;
using BCEntity.OA.Attendance.AttendanceClassSetting;
using BCData.OA.Attendance.AttendanceClass;
using BCData.OA.Attendance.AttendanceClassSetting;
using BCDto.MySql.Admin;
using BCDto.OA.Attendance.AttendanceClass;
using BCDto.OA.Attendance.AttendanceClassSetting;
using BCService.Utils;

namespace BCService.OA.Attendance.AttendanceClass
{
    /// <summary>
    /// 考勤班次信息服务
    /// </summary>
    public class AttendanceClassService : IAttendanceClassService
    {
        private readonly IAttendanceClassData attendanceClassData;
        private readonly IAttendanceClassSettingData attendanceClassSettingData;

        public AttendanceClassService(IAttendanceClassData attendanceClass, IAttendanceClassSettingData attendanceClassSettingData)
        {
            this.attendanceClassData = attendanceClass;
            this.attendanceClassSettingData = attendanceClassSettingData;
        }

        /// <summary>
        /// 新创建班次信息
        /// </summary>
        /// <param name="admin">当前登录管理员</param>
        /// <param name="dto">请求的班次信息</param>
        /// <returns></returns>
        public AttendanceClassDto Create(AdminDto admin, AttendanceClassRequestDto dto)
        {
            //使用事务创建班次信息
            AttendanceClassEntity entity = dto.As<AttendanceClassEntity>();
            entity.AdminId = admin.AdminId;
            entity.AdminName = admin.FullName;
            entity.TotalDays = dto.TotalDays.IsNull() ? 1 : dto.TotalDays.Value;
            entity.TotalWorkHours = dto.TotalWorkHours.IsNull() ? string.Empty : dto.TotalWorkHours;

            List<AttendanceClassSettingEntity> settingEntities = new List<AttendanceClassSettingEntity>();
            foreach (var item in dto.Settings)
            {
                settingEntities.Add(new AttendanceClassSettingEntity()
                {
                    Start = item.Start,
                    End = item.End
                });

                entity.WeekDay += string.Format("{0}-{1} ", item.Start, item.End);
            }
            return attendanceClassData.InsertAsync(entity, settingEntities).GetAwaiter().GetResult().As<AttendanceClassDto>();
        }

        /// <summary>
        /// 获取指定的班次详情信息
        /// </summary>
        /// <param name="classId">班次Id</param>
        /// <returns></returns>

        public AttendanceClassDto GetAttendanceClassByClassId(long classId)
        {
            var result = attendanceClassData.GetEntityAsync(classId).GetAwaiter().GetResult().As<AttendanceClassDto>();
            result.Settings = attendanceClassSettingData.GetSettingsByClassIdAsync(result.ClassId).GetAwaiter().GetResult().As<List<AttendanceClassSettingDto>>();
            return result;
        }

        /// <summary>
        ///删除指定的班次信息
        /// </summary>
        /// <param name="admin">当前管理员</param>
        /// <param name="classId">指定的班次Id</param>
        /// <returns></returns>
        public int Delete(AdminDto admin, long classId)
        {
            if (!attendanceClassData.CheckIsExist(classId).GetAwaiter().GetResult())
            {
                throw new ArgumentException("班次信息不存在");
            }
            //TODO:判断如果该班次下有班组信息也一并删除
            return attendanceClassData.DeleteAsync(classId, admin.FullName).GetAwaiter().GetResult();
        }


        /// <summary>
        /// 分页获取班次信息
        /// </summary>
        /// <param name="ascending">升序</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">当前页面大小</param>
        /// <param name="className"></param>
        /// <returns></returns>
        public PageableList<AttendanceClassDto> GetList(bool? ascending, int pageIndex, int pageSize, string className)
        {
            var (list, count) = attendanceClassData.GetAsync(pageIndex, pageSize, ascending.Value, className).GetAwaiter().GetResult();
            var result = list.As<List<AttendanceClassDto>>();
            foreach (var item in result)
            {
                item.Settings = attendanceClassSettingData.GetSettingsByClassIdAsync(item.ClassId).GetAwaiter().GetResult().As<List<AttendanceClassSettingDto>>();
            }
            return new PageableList<AttendanceClassDto>()
            {
                Count = count,
                Items = result,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }

        /// <summary>
        /// 修改指定的班次信息
        /// </summary>
        /// <param name="admin">当前管理员</param>
        /// <param name="classId">班次Id</param>
        /// <param name="dto">修改信息</param>
        /// <returns></returns>
        public AttendanceClassDto Update(AdminDto admin, long classId, AttendanceClassRequestDto dto)
        {
            var updateEntity = attendanceClassData.GetEntityAsync(classId).GetAwaiter().GetResult();
            if (updateEntity == null)
            {
                throw new ArgumentException("班次信息不存在或已作废");
            }
            AttendanceClassEntity entity = dto.As<AttendanceClassEntity>();
            entity.ClassId = classId;
            entity.ModifierId = admin.AdminId;
            entity.ModifierName = admin.FullName;
            entity.TotalDays = dto.TotalDays.IsNull() ? 1 : dto.TotalDays.Value;
            entity.TotalWorkHours = dto.TotalWorkHours.IsNull() ? string.Empty : dto.TotalWorkHours;
            List<AttendanceClassSettingEntity> settingEntities = new List<AttendanceClassSettingEntity>();
            foreach (var item in dto.Settings)
            {
                settingEntities.Add(new AttendanceClassSettingEntity()
                {
                    Start = item.Start,
                    End = item.End
                });

                entity.WeekDay += string.Format("{0}-{1} ", item.Start, item.End);
            }
            return attendanceClassData.UpdateAsync(entity, settingEntities).GetAwaiter().GetResult().As<AttendanceClassDto>();
        }


        /// <summary>
        /// 获取考勤班次以及班次设置信息
        /// </summary>
        /// <param name="classId">班次Id</param>
        /// <returns></returns>
        public List<AttendanceClassViewDto> GetAttendanceClassWithSettings(long classId)
        {
            return attendanceClassData.GetAttendanceClassWithSettings(classId).GetAwaiter().GetResult().As<List<AttendanceClassViewDto>>();
        }

    }
}
